package com.bbu.javaexam.controller;

import com.bbu.javaexam.domain.*;
import com.bbu.javaexam.service.LoginService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName LoginController.java
 * @Description TODO
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RespData login(@RequestBody Login login) {
        Integer username = login.getUsername();
        String password = login.getPassword();
        Admin admin = loginService.adminLogin(username, password);
        if (admin != null) {
            return RespDataHandler.buildRespData(200, "请求成功", admin);
        }
        Student student = loginService.studentLogin(username, password);
        if (student != null) {
            return RespDataHandler.buildRespData(200, "请求成功", student);
        }
        Teacher teacher = loginService.teacherLogin(username, password);
        if (teacher != null) {
            return RespDataHandler.buildRespData(200, "请求成功", teacher);
        }
        return RespDataHandler.buildRespData(500, "请求失败", null);
    }
}
